<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="霜序廿的个人网站">
  <link 
    rel="icon" 
    href="https://api2.mubu.com/v3/photo/654b368e-b847-4122-982c-86d90b3f5275.jpg">
  <title>Vue 数据更新了但页面没有更新的 7 种情况汇总及延伸总结</title>
  
    
      <meta 
        property="og:title" 
        content="Vue 数据更新了但页面没有更新的 7 种情况汇总及延伸总结">
    
    
      <meta 
        property="og:url" 
        content="https://shuangxunian.github.io/2022/01/15/whyIsTheVueDataUpdatedButNotThePage/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://api2.mubu.com/v3/photo/654b368e-b847-4122-982c-86d90b3f5275.jpg">
    
    
      <meta 
        property="og:img" 
        content="如题目">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2022-01-15">
      <meta 
        property="og:article:modified_time" 
        content="2022-01-15">
      <meta 
        property="og:article:author" 
        content="霜序廿">
      
        
          <meta 
            property="og:article:tag" 
            content="vue">
        
      
    
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
      
      
      
      
        
        
        
        <script>
          function prismThemeChange() {
            if(document.getElementById('theme-color').dataset.mode === 'dark') {
              if(document.querySelector('[data-prism]')) {
                changeCSS('/js/lib/prism/prism-tomorrow.min.css', '[data-prism]', 'prism-tomorrow');
              } else {
                loadCSS('/js/lib/prism/prism-tomorrow.min.css', 'prism', 'prism-tomorrow');
              }
            } else {
              if(document.querySelector('[data-prism]')) {
                changeCSS('/js/lib/prism/prism.min.css', '[data-prism]', 'prism');
              } else {
                loadCSS('/js/lib/prism/prism.min.css', 'prism', 'prism');
              }
            }
          }
          prismThemeChange()
        </script>
      
      
        
        <link rel="stylesheet" href="/js/lib/prism/prism-line-numbers.min.css">
      
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
        prismThemeChange();
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
        prismThemeChange();
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
        prismThemeChange();
      }
    };
    setDarkmode();
    </script>
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
<meta name="generator" content="Hexo 5.4.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <span class="navbar-logo-main">
      
        <img 
          class="navbar-logo-img" 
          src="https://api2.mubu.com/v3/photo/654b368e-b847-4122-982c-86d90b3f5275.jpg" 
          alt="blog logo">
      
      <span class="navbar-logo-dsc">霜序廿的个人网站</span>
    </span>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <a 
      class="navbar-menu-item darknavbar" 
      id="dark">
      <i class="iconfont icon-weather"></i>
    </a>
    <a 
      class="navbar-menu-item searchnavbar" 
      id="search">
      <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i>
    </a>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <main class="main-column">
<article class="card card-content">
  <header>
    <h1 class="post-title">
      Vue 数据更新了但页面没有更新的 7 种情况汇总及延伸总结
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2022-01-15T11:29:27.616Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2022-01-15</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0/" 
          class="post-meta-link">
          技术文章
        </a>
      
    
    
      <span class="dot"></span>
      <span>2.3k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/vue/" 
            class="post-meta-link">
            vue
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>如果你发现你自己需要在 Vue 中做一次强制更新，99.9% 的情况，是你在某个地方做错了事。</p>
<h2 id="Vue-无法检测实例被创建时不存在于-data-中的-property"><a href="#Vue-无法检测实例被创建时不存在于-data-中的-property" class="headerlink" title="Vue 无法检测实例被创建时不存在于 data 中的 property"></a>Vue 无法检测实例被创建时不存在于 <code>data</code> 中的 property</h2><p>原因：由于 Vue 会在初始化实例时对 property 执行 <code>getter/setter</code> 转化，所以 property 必须在 <code>data</code> 对象上存在才能让 Vue 将它转换为响应式的。</p>
<p>场景：</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> vm <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
  data<span class="token operator">:</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  <span class="token comment">// 页面不会变化</span>
  template<span class="token operator">:</span> <span class="token string">'&lt;div>&#123;&#123;message&#125;&#125;&lt;/div>'</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
vm<span class="token punctuation">.</span>message <span class="token operator">=</span> <span class="token string">'Hello!'</span> <span class="token comment">// `vm.message` 不是响应式的</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>解决办法：</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> vm <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
  data<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
    <span class="token comment">// 声明 a、b 为一个空值字符串</span>
    message<span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  template<span class="token operator">:</span> <span class="token string">'&lt;div>&#123;&#123; message &#125;&#125;&lt;/div>'</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
vm<span class="token punctuation">.</span>message <span class="token operator">=</span> <span class="token string">'Hello!'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="Vue-无法检测对象-property-的添加或移除"><a href="#Vue-无法检测对象-property-的添加或移除" class="headerlink" title="Vue 无法检测对象 property 的添加或移除"></a>Vue 无法检测对象 property 的添加或移除</h2><p>原因：官方 - 由于 JavaScript（ES5） 的限制，Vue.js <strong>不能检测到对象属性的添加或删除</strong>。因为 Vue.js 在初始化实例时将属性转为 getter/setter，所以属性必须在 <code>data</code> 对象上才能让 Vue.js 转换它，才能让它是响应的。</p>
<p>场景：</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> vm <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
  data<span class="token operator">:</span><span class="token punctuation">&#123;</span>
    obj<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
      id<span class="token operator">:</span> <span class="token number">001</span>
    <span class="token punctuation">&#125;</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  <span class="token comment">// 页面不会变化</span>
  template<span class="token operator">:</span> <span class="token string">'&lt;div>&#123;&#123; obj.message &#125;&#125;&lt;/div>'</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span>

vm<span class="token punctuation">.</span>obj<span class="token punctuation">.</span>message <span class="token operator">=</span> <span class="token string">'hello'</span> <span class="token comment">// 不是响应式的</span>
<span class="token keyword">delete</span> vm<span class="token punctuation">.</span>obj<span class="token punctuation">.</span>id       <span class="token comment">// 不是响应式的</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>解决办法：</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">// 动态添加 - Vue.set</span>
Vue<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>vm<span class="token punctuation">.</span>obj<span class="token punctuation">,</span> propertyName<span class="token punctuation">,</span> newValue<span class="token punctuation">)</span>

<span class="token comment">// 动态添加 - vm.$set</span>
vm<span class="token punctuation">.</span><span class="token function">$set</span><span class="token punctuation">(</span>vm<span class="token punctuation">.</span>obj<span class="token punctuation">,</span> propertyName<span class="token punctuation">,</span> newValue<span class="token punctuation">)</span>

<span class="token comment">// 动态添加多个</span>
<span class="token comment">// 代替 Object.assign(this.obj, &#123; a: 1, b: 2 &#125;)</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>obj <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>obj<span class="token punctuation">,</span> <span class="token punctuation">&#123;</span> a<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> b<span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">&#125;</span><span class="token punctuation">)</span>

<span class="token comment">// 动态移除 - Vue.delete</span>
Vue<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>vm<span class="token punctuation">.</span>obj<span class="token punctuation">,</span> propertyName<span class="token punctuation">)</span>

<span class="token comment">// 动态移除 - vm.$delete</span>
vm<span class="token punctuation">.</span>$<span class="token keyword">delete</span><span class="token punctuation">(</span>vm<span class="token punctuation">.</span>obj<span class="token punctuation">,</span> propertyName<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="Vue-不能检测通过数组索引直接修改一个数组项"><a href="#Vue-不能检测通过数组索引直接修改一个数组项" class="headerlink" title="Vue 不能检测通过数组索引直接修改一个数组项"></a>Vue 不能检测通过数组索引直接修改一个数组项</h2><p>原因：官方 - 由于 JavaScript 的限制，Vue <strong>不能检测</strong>数组和对象的变化；尤雨溪 - 性能代价和获得用户体验不成正比。</p>
<p>场景：</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> vm <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
  data<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
    items<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">]</span>
  <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
vm<span class="token punctuation">.</span>items<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'x'</span> <span class="token comment">// 不是响应性的</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>解决办法：</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">// Vue.set</span>
Vue<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>vm<span class="token punctuation">.</span>items<span class="token punctuation">,</span> indexOfItem<span class="token punctuation">,</span> newValue<span class="token punctuation">)</span>

<span class="token comment">// vm.$set</span>
vm<span class="token punctuation">.</span><span class="token function">$set</span><span class="token punctuation">(</span>vm<span class="token punctuation">.</span>items<span class="token punctuation">,</span> indexOfItem<span class="token punctuation">,</span> newValue<span class="token punctuation">)</span>

<span class="token comment">// Array.prototype.splice</span>
vm<span class="token punctuation">.</span>items<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span>indexOfItem<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> newValue<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="拓展：Object-defineProperty-可以监测数组的变化"><a href="#拓展：Object-defineProperty-可以监测数组的变化" class="headerlink" title="拓展：Object.defineProperty() 可以监测数组的变化"></a>拓展：Object.defineProperty() 可以监测数组的变化</h2><p>Object.defineProperty() 可以监测数组的变化。但对数组新增一个属性（index）不会监测到数据变化，因为无法监测到新增数组的下标（index），删除一个属性（index）也是。</p>
<p>场景：</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span>
arr<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span> index</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> index<span class="token punctuation">,</span> <span class="token punctuation">&#123;</span>
    <span class="token function-variable function">set</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'触发 setter'</span><span class="token punctuation">)</span>
      item <span class="token operator">=</span> value
    <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
    <span class="token function-variable function">get</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'触发 getter'</span><span class="token punctuation">)</span>
      <span class="token keyword">return</span> item
    <span class="token punctuation">&#125;</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
arr<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'123'</span>  <span class="token comment">// 触发 setter</span>
arr<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>          <span class="token comment">// 触发 getter 返回值为 "123"</span>
arr<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">5</span>      <span class="token comment">// 不会触发 setter 和 getter</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="Vue-不能监测直接修改数组长度的变化"><a href="#Vue-不能监测直接修改数组长度的变化" class="headerlink" title="Vue 不能监测直接修改数组长度的变化"></a>Vue 不能监测直接修改数组长度的变化</h2><p>原因：官方 - 由于 JavaScript 的限制，Vue <strong>不能检测</strong>数组和对象的变化；尤雨溪 - 性能代价和获得用户体验不成正比。</p>
<p>场景：</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> vm <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
  data<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
    items<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">]</span>
  <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
vm<span class="token punctuation">.</span>items<span class="token punctuation">.</span>length <span class="token operator">=</span> <span class="token number">2</span> <span class="token comment">// 不是响应性的</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>解决办法：</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">vm<span class="token punctuation">.</span>items<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span>newLength<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h2 id="在异步更新执行之前操作-DOM-数据不会变化"><a href="#在异步更新执行之前操作-DOM-数据不会变化" class="headerlink" title="在异步更新执行之前操作 DOM 数据不会变化"></a>在异步更新执行之前操作 DOM 数据不会变化</h2><p>原因：Vue 在更新 DOM 时是<strong>异步</strong>执行的。只要侦听到数据变化，Vue 将开启一个队列，并缓冲在同一事件循环中发生的所有数据变更。如果同一个 watcher 被多次触发，只会被推入到队列中一次。这种在缓冲时去除重复数据对于避免不必要的计算和 DOM 操作是非常重要的。然后，在下一个的事件循环“tick”中，Vue 刷新队列并执行实际 (已去重的) 工作。Vue 在内部对异步队列尝试使用原生的 <code>Promise.then</code>、<code>MutationObserver</code> 和 <code>setImmediate</code>，如果执行环境不支持，则会采用 <code>setTimeout(fn, 0)</code> 代替。</p>
<p>场景：</p>
<pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>example<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>&#123;&#123;message&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
var vm = new Vue(&#123;
  el: '#example',
  data: &#123;
    message: '123'
  &#125;
&#125;)
vm.message = 'new message' // 更改数据
vm.$el.textContent === 'new message' // false
vm.$el.style.color = 'red' // 页面没有变化<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>解决办法：</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> vm <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
  el<span class="token operator">:</span> <span class="token string">'#example'</span><span class="token punctuation">,</span>
  data<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
    message<span class="token operator">:</span> <span class="token string">'123'</span>
  <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
vm<span class="token punctuation">.</span>message <span class="token operator">=</span> <span class="token string">'new message'</span> <span class="token comment">// 更改数据</span>
<span class="token comment">//使用 Vue.nextTick(callback) callback 将在 DOM 更新完成后被调用</span>
Vue<span class="token punctuation">.</span><span class="token function">nextTick</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  vm<span class="token punctuation">.</span>$el<span class="token punctuation">.</span>textContent <span class="token operator">===</span> <span class="token string">'new message'</span> <span class="token comment">// true</span>
  vm<span class="token punctuation">.</span>$el<span class="token punctuation">.</span>style<span class="token punctuation">.</span>color <span class="token operator">=</span> <span class="token string">'red'</span> <span class="token comment">// 文字颜色变成红色</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="拓展：异步更新带来的数据响应的误解"><a href="#拓展：异步更新带来的数据响应的误解" class="headerlink" title="拓展：异步更新带来的数据响应的误解"></a>拓展：异步更新带来的数据响应的误解</h2><pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token comment">&lt;!-- 页面显示：我更新啦！ --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>example<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>&#123;&#123;message.text&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
var vm = new Vue(&#123;
  el: '#example',
  data: &#123;
    message: &#123;&#125;,
  &#125;
&#125;)
vm.$nextTick(function () &#123;
  this.message = &#123;&#125;
  this.message.text = '我更新啦！'
&#125;)<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>上段代码中，我们在 <code>data</code> 对象中声明了一个 <code>message</code> 空对象，然后在下次 DOM 更新循环结束之后触发的异步回调中，执行了如下两段代码：</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">this</span><span class="token punctuation">.</span>message <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>message<span class="token punctuation">.</span>text <span class="token operator">=</span> <span class="token string">'我更新啦！'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>到这里，模版更新了，页面最后会显示 <code>我更新啦！</code>。</p>
<p><strong>模板更新了，应该具有响应式特性，如果这么想那么你就已经走入了误区。</strong></p>
<p>一开始我们在 <code>data</code> 对象中只是声明了一个 <code>message</code> 空对象，并不具有 <code>text</code> 属性，所以该 <code>text</code> 属性是不具有响应式特性的。</p>
<p><strong>但模板切切实实已经更新了，这又是怎么回事呢？</strong></p>
<p>那是因为 Vue.js 的 DOM 更新是异步的，即当 <code>setter</code> 操作发生后，指令并不会立马更新，指令的更新操作会有一个延迟，当指令更新真正执行的时候，此时 <code>text</code> 属性已经赋值，所以指令更新模板时得到的是新值。</p>
<blockquote>
<p> 模板中每个指令/数据绑定都有一个对应的 <strong>watcher</strong> 对象，在计算过程中它把属性记录为依赖。之后当依赖的 setter 被调用时，会触发 watcher 重新计算 ，也就会导致它的关联指令更新 DOM。</p>
</blockquote>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://api2.mubu.com/v3/document_image/7771c29c-b6bb-403b-8f05-fa0a955464ca-3807603.jpg" class="lozad post-image"src="https://api2.mubu.com/v3/document_image/7771c29c-b6bb-403b-8f05-fa0a955464ca-3807603.jpg"></p>
<p>具体流程如下所示：</p>
<ul>
<li>执行 <code>this.message = &#123;&#125;;</code> 时， <code>setter</code> 被调用。</li>
<li>Vue.js 追踪到 <code>message</code> 依赖的 <code>setter</code> 被调用后，会触发 <code>watcher</code> 重新计算。</li>
<li><code>this.message.text = &#39;我更新啦！&#39;;</code> 对 <code>text</code> 属性进行赋值。</li>
<li>异步回调逻辑执行结束之后，就会导致它的关联指令更新 DOM，指令更新开始执行。</li>
</ul>
<p>所以真正的触发模版更新的操作是 <code>this.message = &#123;&#125;;</code>这一句引起的，因为触发了 <code>setter</code>，所以单看上述例子，具有响应式特性的数据只有 <code>message</code> 这一层，它的动态添加的属性是不具备的。</p>
<blockquote>
<p>对应上述第二点 - Vue 无法检测对象 property 的添加或移除</p>
</blockquote>
<h2 id="循环嵌套层级太深，视图不更新？"><a href="#循环嵌套层级太深，视图不更新？" class="headerlink" title="循环嵌套层级太深，视图不更新？"></a>循环嵌套层级太深，视图不更新？</h2><p>看到网上有些人说数据更新的层级太深，导致数据不更新或者更新缓慢从而导致试图不更新？</p>
<p>由于我没有遇到过这种情况，在我试图重现这种场景的情况下，发现并没有上述情况的发生，所以对于这一点不进行过多描述（如果有人在真实场景下遇到这种情况留个言吧）。</p>
<p>针对上述情况有人给出的解决方案是使用强制更新：</p>
<blockquote>
<p>如果你发现你自己需要在 Vue 中做一次强制更新，99.9% 的情况，是你在某个地方做错了事。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">vm<span class="token punctuation">.</span><span class="token function">$forceUpdate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h2 id="拓展：路由参数变化时，页面不更新（数据不更新）"><a href="#拓展：路由参数变化时，页面不更新（数据不更新）" class="headerlink" title="拓展：路由参数变化时，页面不更新（数据不更新）"></a>拓展：路由参数变化时，页面不更新（数据不更新）</h2><p>拓展一个因为路由参数变化，而导致页面不更新的问题，页面不更新本质上就是数据没有更新。</p>
<blockquote>
<p>原因：路由视图组件<strong>引用了相同组件</strong>时，当路由参会变化时，会导致该组件无法更新，也就是我们常说中的页面无法更新的问题。</p>
</blockquote>
<p>场景：</p>
<pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>app<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/home/foo<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>To Foo<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span>    
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/home/baz<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>To Baz<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span>    
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/home/bar<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>To Bar<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span>    
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">></span></span>    
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-view</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-view</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
const Home = &#123;
  template: `<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">></span></span>&#123;&#123;message&#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>`,
  data() &#123;
    return &#123;
      message: this.$route.params.name
    &#125;
  &#125;
&#125;

const router = new VueRouter(&#123;
  mode:'history',
    routes: [
    &#123;path: '/home', component: Home &#125;,
    &#123;path: '/home/:name', component: Home &#125;
  ]
&#125;)

new Vue(&#123;
  el: '#app',
  router
&#125;)<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>上段代码中，我们在路由构建选项 <code>routes</code> 中配置了一个动态路由 <code>&#39;/home/:name&#39;</code>，它们共用一个路由组件 <code>Home</code>，这代表他们复用 <code>RouterView</code> 。</p>
<p>当进行路由切换时，页面只会渲染第一次路由匹配到的参数，之后再进行路由切换时，<code>message</code> 是没有变化的。</p>
<p>解决办法：</p>
<blockquote>
<p>解决的办法有很多种，这里只列举我常用到几种方法。</p>
</blockquote>
<ol>
<li><p>通过 <code>watch</code> 监听 <code>$route</code> 的变化。</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">const</span> Home <span class="token operator">=</span> <span class="token punctuation">&#123;</span>
  template<span class="token operator">:</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">&lt;div>&#123;&#123;message&#125;&#125;&lt;/div></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">,</span>
  <span class="token function">data</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">return</span> <span class="token punctuation">&#123;</span>
      message<span class="token operator">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$route<span class="token punctuation">.</span>params<span class="token punctuation">.</span>name
    <span class="token punctuation">&#125;</span>
  <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
  watch<span class="token operator">:</span> <span class="token punctuation">&#123;</span>
       <span class="token string">'$route'</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
       <span class="token keyword">this</span><span class="token punctuation">.</span>message <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$route<span class="token punctuation">.</span>params<span class="token punctuation">.</span>name
    <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token operator">...</span>
<span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
  el<span class="token operator">:</span> <span class="token string">'#app'</span><span class="token punctuation">,</span>
  router
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li>
<li><p>给 <code>&lt;router-view&gt;</code> 绑定 <code>key</code> 属性，这样 Vue 就会认为这是不同的 <code>&lt;router-view&gt;</code>。</p>
<blockquote>
<p>弊端：如果从 <code>/home</code> 跳转到 <code>/user</code> 等其他路由下，我们是不用担心组件更新问题的，所以这个时候 <code>key</code> 属性是多余的。</p>
</blockquote>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token operator">&lt;</span>div id<span class="token operator">=</span><span class="token string">"app"</span><span class="token operator">></span>
  <span class="token operator">...</span>
  <span class="token operator">&lt;</span>router<span class="token operator">-</span>view <span class="token operator">:</span>key<span class="token operator">=</span><span class="token string">"key"</span><span class="token operator">></span><span class="token operator">&lt;</span><span class="token operator">/</span>router<span class="token operator">-</span>view<span class="token operator">></span>
<span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></li>
</ol>
<p>参考：</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://link.segmentfault.com/?enc=KS58sTB6fZDfqLo+V9E9Ng==.8PheIBZuDl3IKkLKEfwaNUtKwaN+74A2iRMrzI6A6uXA43EYvjlIYh+1c6Ao0OphWAkw8h3JbeoIXX8iy1IfEQ==">对 Vue 响应式数据更新的误解</a></li>
<li><a target="_blank" rel="noopener" href="https://link.segmentfault.com/?enc=oZjKUIxE/mJ4pCUwJjR5Hw==.ENnQSKGEs+afZ0U8mtrTnAfrUYx3eU8Dkzs91IG8LFP+nhF8w8UJPWE9RQxvorFL">小丸子的城堡</a></li>
<li><a target="_blank" rel="noopener" href="https://segmentfault.com/a/1190000022772025">Vue 数据更新了但页面没有更新的 7 种情况汇总及延伸总结</a></li>
</ul>

  </div>
  <div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2022/01/15/vueComponentsCommunicateWithEachOtherSiblingComponentsCommunicate/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2022/01/15/theExtendExtendsMinxinMinxinsUsageAndDifference/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">Vue中extend,extends,minxin,minxins的用法和区别 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue-%E6%97%A0%E6%B3%95%E6%A3%80%E6%B5%8B%E5%AE%9E%E4%BE%8B%E8%A2%AB%E5%88%9B%E5%BB%BA%E6%97%B6%E4%B8%8D%E5%AD%98%E5%9C%A8%E4%BA%8E-data-%E4%B8%AD%E7%9A%84-property"><span class="toc-text">Vue 无法检测实例被创建时不存在于 data 中的 property</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue-%E6%97%A0%E6%B3%95%E6%A3%80%E6%B5%8B%E5%AF%B9%E8%B1%A1-property-%E7%9A%84%E6%B7%BB%E5%8A%A0%E6%88%96%E7%A7%BB%E9%99%A4"><span class="toc-text">Vue 无法检测对象 property 的添加或移除</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue-%E4%B8%8D%E8%83%BD%E6%A3%80%E6%B5%8B%E9%80%9A%E8%BF%87%E6%95%B0%E7%BB%84%E7%B4%A2%E5%BC%95%E7%9B%B4%E6%8E%A5%E4%BF%AE%E6%94%B9%E4%B8%80%E4%B8%AA%E6%95%B0%E7%BB%84%E9%A1%B9"><span class="toc-text">Vue 不能检测通过数组索引直接修改一个数组项</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8B%93%E5%B1%95%EF%BC%9AObject-defineProperty-%E5%8F%AF%E4%BB%A5%E7%9B%91%E6%B5%8B%E6%95%B0%E7%BB%84%E7%9A%84%E5%8F%98%E5%8C%96"><span class="toc-text">拓展：Object.defineProperty() 可以监测数组的变化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue-%E4%B8%8D%E8%83%BD%E7%9B%91%E6%B5%8B%E7%9B%B4%E6%8E%A5%E4%BF%AE%E6%94%B9%E6%95%B0%E7%BB%84%E9%95%BF%E5%BA%A6%E7%9A%84%E5%8F%98%E5%8C%96"><span class="toc-text">Vue 不能监测直接修改数组长度的变化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9C%A8%E5%BC%82%E6%AD%A5%E6%9B%B4%E6%96%B0%E6%89%A7%E8%A1%8C%E4%B9%8B%E5%89%8D%E6%93%8D%E4%BD%9C-DOM-%E6%95%B0%E6%8D%AE%E4%B8%8D%E4%BC%9A%E5%8F%98%E5%8C%96"><span class="toc-text">在异步更新执行之前操作 DOM 数据不会变化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8B%93%E5%B1%95%EF%BC%9A%E5%BC%82%E6%AD%A5%E6%9B%B4%E6%96%B0%E5%B8%A6%E6%9D%A5%E7%9A%84%E6%95%B0%E6%8D%AE%E5%93%8D%E5%BA%94%E7%9A%84%E8%AF%AF%E8%A7%A3"><span class="toc-text">拓展：异步更新带来的数据响应的误解</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BE%AA%E7%8E%AF%E5%B5%8C%E5%A5%97%E5%B1%82%E7%BA%A7%E5%A4%AA%E6%B7%B1%EF%BC%8C%E8%A7%86%E5%9B%BE%E4%B8%8D%E6%9B%B4%E6%96%B0%EF%BC%9F"><span class="toc-text">循环嵌套层级太深，视图不更新？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8B%93%E5%B1%95%EF%BC%9A%E8%B7%AF%E7%94%B1%E5%8F%82%E6%95%B0%E5%8F%98%E5%8C%96%E6%97%B6%EF%BC%8C%E9%A1%B5%E9%9D%A2%E4%B8%8D%E6%9B%B4%E6%96%B0%EF%BC%88%E6%95%B0%E6%8D%AE%E4%B8%8D%E6%9B%B4%E6%96%B0%EF%BC%89"><span class="toc-text">拓展：路由参数变化时，页面不更新（数据不更新）</span></a></li></ol>
</div></main>
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://api2.mubu.com/v3/photo/654b368e-b847-4122-982c-86d90b3f5275.jpg" 
    class="author-img" 
    alt="author avatar">

<p class="author-name">霜序廿</p>
<p class="author-description">无限进步</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>253</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>6</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>51</span>
    <span>标签</span>
  </a>
</div>

  <div class="author-card-society">
    
      <div class="author-card-society-icon">
        <a target="_blank" rel="noopener" href="https://github.com/shuangxunian">
          <i class="iconfont icon-github society-icon"></i>
        </a>
      </div>
    
      <div class="author-card-society-icon">
        <a target="_blank" rel="noopener" href="https://space.bilibili.com/391117803">
          <i class="iconfont icon-bilibili society-icon"></i>
        </a>
      </div>
    
  </div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue-%E6%97%A0%E6%B3%95%E6%A3%80%E6%B5%8B%E5%AE%9E%E4%BE%8B%E8%A2%AB%E5%88%9B%E5%BB%BA%E6%97%B6%E4%B8%8D%E5%AD%98%E5%9C%A8%E4%BA%8E-data-%E4%B8%AD%E7%9A%84-property"><span class="toc-text">Vue 无法检测实例被创建时不存在于 data 中的 property</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue-%E6%97%A0%E6%B3%95%E6%A3%80%E6%B5%8B%E5%AF%B9%E8%B1%A1-property-%E7%9A%84%E6%B7%BB%E5%8A%A0%E6%88%96%E7%A7%BB%E9%99%A4"><span class="toc-text">Vue 无法检测对象 property 的添加或移除</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue-%E4%B8%8D%E8%83%BD%E6%A3%80%E6%B5%8B%E9%80%9A%E8%BF%87%E6%95%B0%E7%BB%84%E7%B4%A2%E5%BC%95%E7%9B%B4%E6%8E%A5%E4%BF%AE%E6%94%B9%E4%B8%80%E4%B8%AA%E6%95%B0%E7%BB%84%E9%A1%B9"><span class="toc-text">Vue 不能检测通过数组索引直接修改一个数组项</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8B%93%E5%B1%95%EF%BC%9AObject-defineProperty-%E5%8F%AF%E4%BB%A5%E7%9B%91%E6%B5%8B%E6%95%B0%E7%BB%84%E7%9A%84%E5%8F%98%E5%8C%96"><span class="toc-text">拓展：Object.defineProperty() 可以监测数组的变化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue-%E4%B8%8D%E8%83%BD%E7%9B%91%E6%B5%8B%E7%9B%B4%E6%8E%A5%E4%BF%AE%E6%94%B9%E6%95%B0%E7%BB%84%E9%95%BF%E5%BA%A6%E7%9A%84%E5%8F%98%E5%8C%96"><span class="toc-text">Vue 不能监测直接修改数组长度的变化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9C%A8%E5%BC%82%E6%AD%A5%E6%9B%B4%E6%96%B0%E6%89%A7%E8%A1%8C%E4%B9%8B%E5%89%8D%E6%93%8D%E4%BD%9C-DOM-%E6%95%B0%E6%8D%AE%E4%B8%8D%E4%BC%9A%E5%8F%98%E5%8C%96"><span class="toc-text">在异步更新执行之前操作 DOM 数据不会变化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8B%93%E5%B1%95%EF%BC%9A%E5%BC%82%E6%AD%A5%E6%9B%B4%E6%96%B0%E5%B8%A6%E6%9D%A5%E7%9A%84%E6%95%B0%E6%8D%AE%E5%93%8D%E5%BA%94%E7%9A%84%E8%AF%AF%E8%A7%A3"><span class="toc-text">拓展：异步更新带来的数据响应的误解</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BE%AA%E7%8E%AF%E5%B5%8C%E5%A5%97%E5%B1%82%E7%BA%A7%E5%A4%AA%E6%B7%B1%EF%BC%8C%E8%A7%86%E5%9B%BE%E4%B8%8D%E6%9B%B4%E6%96%B0%EF%BC%9F"><span class="toc-text">循环嵌套层级太深，视图不更新？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8B%93%E5%B1%95%EF%BC%9A%E8%B7%AF%E7%94%B1%E5%8F%82%E6%95%B0%E5%8F%98%E5%8C%96%E6%97%B6%EF%BC%8C%E9%A1%B5%E9%9D%A2%E4%B8%8D%E6%9B%B4%E6%96%B0%EF%BC%88%E6%95%B0%E6%8D%AE%E4%B8%8D%E6%9B%B4%E6%96%B0%EF%BC%89"><span class="toc-text">拓展：路由参数变化时，页面不更新（数据不更新）</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0/">
        <div class="categories-list-item">
          技术文章
          <span class="categories-list-item-badge">218</span>
        </div>
      </a>
    
      <a href="/categories/%E5%85%B6%E4%BB%96/">
        <div class="categories-list-item">
          其他
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/%E6%97%85%E6%B8%B8/">
        <div class="categories-list-item">
          旅游
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/%E7%AE%97%E6%B3%95/">
        <div class="categories-list-item">
          算法
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
      <a href="/categories/%E8%80%83%E8%AF%95/">
        <div class="categories-list-item">
          考试
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
      <a href="/categories/%E9%98%85%E8%AF%BB/">
        <div class="categories-list-item">
          阅读
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/js/" 
        title="js">
        <div class="tags-list-item">js</div>
      </a>
    
      <a 
        href="/tags/vue/" 
        title="vue">
        <div class="tags-list-item">vue</div>
      </a>
    
      <a 
        href="/tags/%E9%9D%A2%E8%AF%95/" 
        title="面试">
        <div class="tags-list-item">面试</div>
      </a>
    
      <a 
        href="/tags/css/" 
        title="css">
        <div class="tags-list-item">css</div>
      </a>
    
      <a 
        href="/tags/%E7%BD%91%E7%BB%9C/" 
        title="网络">
        <div class="tags-list-item">网络</div>
      </a>
    
      <a 
        href="/tags/%E5%85%B6%E4%BB%96/" 
        title="其他">
        <div class="tags-list-item">其他</div>
      </a>
    
      <a 
        href="/tags/%E7%AE%97%E6%B3%95/" 
        title="算法">
        <div class="tags-list-item">算法</div>
      </a>
    
      <a 
        href="/tags/%E6%B5%8F%E8%A7%88%E5%99%A8/" 
        title="浏览器">
        <div class="tags-list-item">浏览器</div>
      </a>
    
      <a 
        href="/tags/html/" 
        title="html">
        <div class="tags-list-item">html</div>
      </a>
    
      <a 
        href="/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" 
        title="操作系统">
        <div class="tags-list-item">操作系统</div>
      </a>
    
      <a 
        href="/tags/%E8%80%83%E8%AF%95/" 
        title="考试">
        <div class="tags-list-item">考试</div>
      </a>
    
      <a 
        href="/tags/%E8%BD%AF%E5%AE%9E%E5%8A%9B/" 
        title="软实力">
        <div class="tags-list-item">软实力</div>
      </a>
    
      <a 
        href="/tags/DOM/" 
        title="DOM">
        <div class="tags-list-item">DOM</div>
      </a>
    
      <a 
        href="/tags/%E8%BD%AE%E5%AD%90/" 
        title="轮子">
        <div class="tags-list-item">轮子</div>
      </a>
    
      <a 
        href="/tags/%E7%BD%91%E7%BB%9C%E5%8E%9F%E7%90%86/" 
        title="网络原理">
        <div class="tags-list-item">网络原理</div>
      </a>
    
      <a 
        href="/tags/debug/" 
        title="debug">
        <div class="tags-list-item">debug</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue-%E6%97%A0%E6%B3%95%E6%A3%80%E6%B5%8B%E5%AE%9E%E4%BE%8B%E8%A2%AB%E5%88%9B%E5%BB%BA%E6%97%B6%E4%B8%8D%E5%AD%98%E5%9C%A8%E4%BA%8E-data-%E4%B8%AD%E7%9A%84-property"><span class="toc-text">Vue 无法检测实例被创建时不存在于 data 中的 property</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue-%E6%97%A0%E6%B3%95%E6%A3%80%E6%B5%8B%E5%AF%B9%E8%B1%A1-property-%E7%9A%84%E6%B7%BB%E5%8A%A0%E6%88%96%E7%A7%BB%E9%99%A4"><span class="toc-text">Vue 无法检测对象 property 的添加或移除</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue-%E4%B8%8D%E8%83%BD%E6%A3%80%E6%B5%8B%E9%80%9A%E8%BF%87%E6%95%B0%E7%BB%84%E7%B4%A2%E5%BC%95%E7%9B%B4%E6%8E%A5%E4%BF%AE%E6%94%B9%E4%B8%80%E4%B8%AA%E6%95%B0%E7%BB%84%E9%A1%B9"><span class="toc-text">Vue 不能检测通过数组索引直接修改一个数组项</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8B%93%E5%B1%95%EF%BC%9AObject-defineProperty-%E5%8F%AF%E4%BB%A5%E7%9B%91%E6%B5%8B%E6%95%B0%E7%BB%84%E7%9A%84%E5%8F%98%E5%8C%96"><span class="toc-text">拓展：Object.defineProperty() 可以监测数组的变化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Vue-%E4%B8%8D%E8%83%BD%E7%9B%91%E6%B5%8B%E7%9B%B4%E6%8E%A5%E4%BF%AE%E6%94%B9%E6%95%B0%E7%BB%84%E9%95%BF%E5%BA%A6%E7%9A%84%E5%8F%98%E5%8C%96"><span class="toc-text">Vue 不能监测直接修改数组长度的变化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9C%A8%E5%BC%82%E6%AD%A5%E6%9B%B4%E6%96%B0%E6%89%A7%E8%A1%8C%E4%B9%8B%E5%89%8D%E6%93%8D%E4%BD%9C-DOM-%E6%95%B0%E6%8D%AE%E4%B8%8D%E4%BC%9A%E5%8F%98%E5%8C%96"><span class="toc-text">在异步更新执行之前操作 DOM 数据不会变化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8B%93%E5%B1%95%EF%BC%9A%E5%BC%82%E6%AD%A5%E6%9B%B4%E6%96%B0%E5%B8%A6%E6%9D%A5%E7%9A%84%E6%95%B0%E6%8D%AE%E5%93%8D%E5%BA%94%E7%9A%84%E8%AF%AF%E8%A7%A3"><span class="toc-text">拓展：异步更新带来的数据响应的误解</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BE%AA%E7%8E%AF%E5%B5%8C%E5%A5%97%E5%B1%82%E7%BA%A7%E5%A4%AA%E6%B7%B1%EF%BC%8C%E8%A7%86%E5%9B%BE%E4%B8%8D%E6%9B%B4%E6%96%B0%EF%BC%9F"><span class="toc-text">循环嵌套层级太深，视图不更新？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8B%93%E5%B1%95%EF%BC%9A%E8%B7%AF%E7%94%B1%E5%8F%82%E6%95%B0%E5%8F%98%E5%8C%96%E6%97%B6%EF%BC%8C%E9%A1%B5%E9%9D%A2%E4%B8%8D%E6%9B%B4%E6%96%B0%EF%BC%88%E6%95%B0%E6%8D%AE%E4%B8%8D%E6%9B%B4%E6%96%B0%EF%BC%89"><span class="toc-text">拓展：路由参数变化时，页面不更新（数据不更新）</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2022-05-01</div>
        <a href="/2022/05/01/typescriptHome/"><div class="recent-posts-item-content">typescript</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2022-01-15</div>
        <a href="/2022/01/15/javaScriptTheVariousWaysAndAdvantagesAndDisadvantagesOfDeepInheritance/"><div class="recent-posts-item-content">JavaScript深入之继承的多种方式和优缺点</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2022-01-15</div>
        <a href="/2022/01/15/javaScriptGoFromPrototypeToPrototypeChain/"><div class="recent-posts-item-content">JavaScript深入之从原型到原型链</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2022-01-15</div>
        <a href="/2022/01/15/javaScriptMemoryLeakTutorial/"><div class="recent-posts-item-content">JavaScript 内存泄漏教程</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    关注嘉然！顿顿解馋！
  </div>
  <div class="recent-posts-list">
    
      <img 
        src="https://api2.mubu.com/v3/document_image/2697c6ae-10ee-41a3-9099-304bdb252d31-3807603.jpg" 
        class="myadd-img" 
        alt="author avatar">
    
  </div>
</div>
  </article>
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2020 -
          
          2022
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          霜序廿的个人网站
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer> 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton" 
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
     
    
    
  </body>
</html>
